﻿using System;
using System.Collections.Generic;
using System.Linq;
using Alphaleonis.Win32.Filesystem;

namespace CueSheetLibrary
{
	public class CueSheet {
		public string Performer;
		public string Title;
		public string Filename;
		public CueLine[] Lines;
		public CueTrack[] Tracks;

		public CueSheet(string cueFile) {
			List<CueTrack> tracks = new List<CueTrack>();
			Lines = File.ReadAllLines(cueFile).Select(l => new CueLine(l)).ToArray();
			CueTrack currentTrack = null;
			foreach(var line in Lines) {
				if(line.Command == "REM") {
				} else if(line.Command == "PERFORMER") {
					if(currentTrack == null) {
						Performer = line[1];
					} else {
						currentTrack.Performer = line[1];
					}
				} else if(line.Command == "TITLE") {
					if(currentTrack == null) {
						Title = line[1];
					} else {
						currentTrack.Title = line[1];
					}
				} else if(line.Command == "FILE") {
					Filename = Path.Combine(Path.GetDirectoryName(cueFile), line[1]);
				} else if(line.Command == "TRACK") {
					int track = int.Parse(line[1]);
					if(track != tracks.Count + 1) {
						throw new FormatException();
					}
					currentTrack = new CueTrack();
					tracks.Add(currentTrack);
				} else if(line.Command == "INDEX") {
					int index = int.Parse(line[1]);

					long time = ParseCdTime(line[2]);
					currentTrack.Indexes[index] = time;
				}
				Tracks = tracks.ToArray();
			}
		}

		public static long ParseCdTime(string time) {
			string[] parts = time.Split(':');
			if(parts.Length != 3) {
				throw new FormatException();
			}
			long[] longs = parts.Select(p => long.Parse(p)).ToArray();
			long frames = 60 * 75 * longs[0] + 75 * longs[1] + longs[2];
			return frames;
		}
	}
}
